package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.service.UserPaidTrackService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	private UserPaidTrackService userPaidTrackService;

	/**
	 * 更新用户会员状态
	 */
	@PostMapping("/userInfo/updateVipExpireStatus")
	public Result updateVipExpireStatus() {
		userInfoService.updateVipExpireStatus();
		return Result.ok();
	}

	/**
	 * 新增购买记录
	 * @param userPaidRecordVo
	 * @return
	 */
	@PostMapping("/userInfo/savePaidRecord")
	public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
		userInfoService.savePaidRecord(userPaidRecordVo);
		return Result.ok();
	}

	/**
	 * /api/user/userInfo/findUserPaidTrackList/{albumId}
	 * 根据专辑id+用户ID获取用户已购买声音id列表
	 * @param albumId
	 * @return
	 */
	@GetMapping("/trackInfo/findUserPaidTrackList/{albumId}")
	@GuiGuLogin
	public Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId) {
		List<Long> result = userPaidTrackService.findUserPaidTrackList(albumId);
		return Result.ok(result);
	}


	/**
	 * /api/user/userInfo/isPaidAlbum/{albumId}
	 * 判断用户是否购买过指定专辑
	 * @param albumId
	 * @return
	 */
	@GetMapping("/userInfo/isPaidAlbum/{albumId}")
	@GuiGuLogin
	public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
		Boolean result = userInfoService.getUserIsPaidAlbum(albumId);
		return Result.ok(result);
	}


	/**
	 * /api/user/userInfo/userIsPaidTrack/{userId}/{albumId}
	 * 获取用户声音列表付费情况
	 * @param userId
	 * @param albumId
	 * @param trackIdList
	 * @return
	 */
	@PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
	public Result<Map<Long, Integer>> getUserIsPaidTrack(@PathVariable Long userId, @PathVariable Long albumId, @RequestBody List<Long> trackIdList) {
		Map<Long, Integer> mapResult = userInfoService.getUserIsPaidTrack(userId, albumId, trackIdList);
		return Result.ok(mapResult);
	}

	/**
	 * /api/user/userInfo/getUserInfoVo/{userId}
	 * 根据用户ID查询用户信息
	 * @param userId
	 * @return
	 */
	@GetMapping("/userInfo/getUserInfoVo/{userId}")
	@GuiGuCache(prefix = "user:info:")
	public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId){
		UserInfoVo userInfoVo = userInfoService.getUserInfoVo(userId);
		return Result.ok(userInfoVo);
	}

}

